﻿
module Sustainalytics.Account {

    import Common = Sustainalytics.Common;

    // Account controller
    export class AccountController {
        
        public researchProducts: AccountPermissionDto[];
        public productInvolvementPermissions: ProductPermissionDto;
        public globalCompactPermissions: ProductPermissionDto;
        public features: AccountPermissionDto[];

        private accountId: string;
        private userId: string;

        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            private AccountService: AccountService,
            private $modal: Common.IModal
            ) {

            var ctx = this;

            if (!ctx.$rootScope['userInfo']) {
                ctx.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }

            if (ctx.$rootScope['userInfo']) {
                ctx.accountId = ctx.$rootScope['userInfo'].accountId;
                ctx.userId = ctx.$rootScope['userInfo'].userId;
            }

            $scope['home'] = ctx;

            if (ctx.accountId && ctx.userId) {

                ctx.getAccountPermissions();
                ctx.getProductPermissions();
            }
        }

        public getProductPermissions() {
            var ctx = this;

            ctx.$rootScope['loading'] = true;

            var promise: ng.IPromise<ProductPermissionDto> = ctx.AccountService.getProductPermission(ctx.accountId, "Product Involvement");

            promise.then(function (obj) {

                ctx.productInvolvementPermissions = obj;
            });

            var promise: ng.IPromise<ProductPermissionDto> = ctx.AccountService.getProductPermission(ctx.accountId, "Global Compact");

            promise.then(function(obj) {
                ctx.globalCompactPermissions = obj;
            });

            ctx.$rootScope['loading'] = false;
        }

        public getAccountPermissions() {
        
            var ctx = this;

            ctx.$rootScope['loading'] = true;

            var promise: ng.IPromise<AccountPermissionDto[]> = ctx.AccountService.getAccountPermissions(ctx.accountId, PermissionTypeEnum.ResearchProduct);

            promise.then(function (list) {

                for (var i = 0; i < list.length; i++) {
                    if (list[i].name === "ESG Research") {
                        list[i].name = "ESG Research & Ratings";
                    }
                    if (list[i].name === "Corporate Governance") {
                        list[i].name = "Corporate Governance Research & Ratings";
                    }
                }

                ctx.researchProducts = list;
            });

            var promise: ng.IPromise<AccountPermissionDto[]> = ctx.AccountService.getAccountPermissions(ctx.accountId, PermissionTypeEnum.Features);

            promise.then(function (list) {
                ctx.features = list;
            });

            ctx.$rootScope['loading'] = false;
        }

        private getIndices(productName: string, isActive: boolean): any {
            var ctx = this;

            var indices: string[] = [];

            if (ctx.researchProducts) {
                $.each(ctx.researchProducts, function(index, rp) {

                    if (rp.name === productName && rp.isActive) {
                        indices = rp.coveredIndices;
                    }
                    if (rp.name === productName && !rp.isActive) {
                        indices = rp.uncoveredIndices;
                    }
                });
            }

            return indices;
        }

        private getPIClaims(isActive: boolean): any {
            var ctx = this;

            var claims: string[] = [];

            if (ctx.productInvolvementPermissions) {
                if (isActive) {
                    claims = ctx.productInvolvementPermissions.covered;
                }
                if (!isActive) {
                    claims = ctx.productInvolvementPermissions.uncovered;
                }
            }

            return claims;
        }

        //private redirectToCompanyPage(companyName: string) {
        //    var ctx = this;

        //    ctx.$location.path('/research/overview/' + companyName);
        //}
    }

    // register controller 
    var account = angular.module('Sustainalytics.Account')
        .controller('AccountController', ['$rootScope', '$scope', '$q', '$location', 'AccountService', '$modal', AccountController]);    

} 